import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/auth.config';
import { prisma } from '@/lib/prisma';
import { redirect } from 'next/navigation';
import { TripList } from '../components/TripList';
import Link from 'next/link';
import { unstable_noStore as noStore } from 'next/cache';

// 禁用页面缓存
export const dynamic = 'force-dynamic';
// 或者使用 revalidate
// export const revalidate = 0;

export default async function TripsPage() {
  // 禁用缓存
  noStore();
  
  const session = await getServerSession(authOptions);

  if (!session?.user) {
    redirect('/api/auth/signin');
  }

  const trips = await prisma.trip.findMany({
    where: {
      userId: session.user.id,
      isOfficial: false,
    },
    orderBy: {
      createdAt: 'desc'
    },
    include: {
      activities: {
        orderBy: {
          date: 'asc'
        }
      },
      user: {
        select: {
          name: true,
          username: true,
        }
      },
      _count: {
        select: {
          activities: true,
          savedBy: true,
          comments: true,
        }
      }
    }
  });

  return (
    <div className="container mx-auto px-4 py-8">
      <div className="flex justify-between items-center mb-8">
        <h1 className="text-3xl font-medium text-blue-900">我的行程</h1>
        <Link
          href="/trips/new"
          className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors"
        >
          创建行程
        </Link>
      </div>

      {trips.length > 0 ? (
        <TripList trips={trips} />
      ) : (
        <div className="text-center py-12 bg-white rounded-lg shadow-lg border border-gray-200">
          <p className="text-gray-500 mb-4">还没有创建任何行程</p>
          <Link
            href="/trips/new"
            className="inline-block px-6 py-3 bg-blue-600 text-white rounded-md hover:bg-blue-700 transition-colors"
          >
            开始创建第一个行程
          </Link>
        </div>
      )}
    </div>
  );
} 